package com.sakura.seckill.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 * MD5两次加密
 * @author Sakura
 * @Date 2022-07-26 17:12
 */

@Component
public class MD5Util {

    //调用MD5加密
    public static String md5(String password){
        return DigestUtils.md5Hex(password);
    }

    //设置盐，进行加密
    private static final String salt = "1a2b3c4d";

    //第一次加密：前端输入密码后端加密
    public static String inputPassToFromPass(String inputPass){
        String password=""+salt.charAt(0)+salt.charAt(2)+inputPass+salt.charAt(5)+salt.charAt(4);
        return md5(password);
    }

    //第二次加密:后端保存到数据库时加密
    public static String fromPassToDBPass(String fromPass,String salt){
        String password=""+salt.charAt(0)+salt.charAt(2)+fromPass+salt.charAt(5)+salt.charAt(4);
        return md5(password);
    }

    //两次加密操作
    public static String inputPassToDBPass(String inputPass,String salt){
        String fromPass = inputPassToFromPass(inputPass);
        String daPass = fromPassToDBPass(fromPass, salt);
        return daPass;
    }

    public static void main(String[] args) {
        //d3b1294a61a07da9b49b6e22b2cbd7f9
        System.out.println(inputPassToFromPass("123456"));
        //0687f9701bca74827fcefcd7e743d179
        System.out.println(fromPassToDBPass("d3b1294a61a07da9b49b6e22b2cbd7f9","1a2b3c4d"));
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));
    }
}
